﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace ReportForm.BLL
{
    class tReportStock
    {
        UTLDB.UTLDB connect = new UTLDB.UTLDB();
        DataTable data = new DataTable();
        DataTable dataSQL = new DataTable();

        public tReportStock() {
            DataColumn[] dc = new DataColumn[]{ 
                                new DataColumn("no", Type.GetType("System.Int32")),
                                new DataColumn("date", Type.GetType("System.String")),
                                new DataColumn("input", Type.GetType("System.Int32")),
                                new DataColumn("output", Type.GetType("System.Int32")),
                                new DataColumn("stock", Type.GetType("System.Int32")),

                                new DataColumn("input1", Type.GetType("System.Double")),
                                new DataColumn("output1", Type.GetType("System.Double")),
                                new DataColumn("stock1", Type.GetType("System.Double")),
                                new DataColumn("input2", Type.GetType("System.String")),
                                new DataColumn("output2", Type.GetType("System.String")),
                                new DataColumn("stock2", Type.GetType("System.String")),
                                new DataColumn("stock_old", Type.GetType("System.Double")),
                                new DataColumn("stock_old1", Type.GetType("System.Double")),
                                new DataColumn("stock_old2", Type.GetType("System.Double")),
                                new DataColumn("namecat", Type.GetType("System.String"))
            };
            data.Columns.AddRange(dc);
        }

        public DataTable GetStock(int id, string from, string to)
        {
            DataTable data = connect.GetDataByStoredProcedure("sp_tmp",
               new string[] { "@idcat", "@tungay", "@denngay" },
               new object[] { id,to,from }
                );
            return data;
        }
        public DataTable SetData(int id, string from, string to)
        {
            
            int i = 0;
            dataSQL = GetStock(id, from, to);

            for (i = 0; i < dataSQL.Rows.Count; i++)
            {
                DataRow r = data.NewRow();

                r["no"] = dataSQL.Rows[i]["no"];
                r["date"] = dataSQL.Rows[i]["date"];
                r["input"] = dataSQL.Rows[i]["input"];
                r["output"] = dataSQL.Rows[i]["output"];
                r["stock"] = dataSQL.Rows[i]["stock"];
                r["input1"] = dataSQL.Rows[i]["input1"];
                r["output1"] = dataSQL.Rows[i]["output1"];
                r["stock1"] = dataSQL.Rows[i]["stock1"];
                r["input2"] =FormatWeight(dataSQL.Rows[i]["input2"].ToString());
                r["output2"] = FormatWeight(dataSQL.Rows[i]["output2"].ToString());
                r["stock2"] = FormatWeight(dataSQL.Rows[i]["stock2"].ToString());
                r["stock_old"] = dataSQL.Rows[i]["stock_old"];
                r["stock_old1"] = dataSQL.Rows[i]["stock_old1"];
                r["stock_old2"] = dataSQL.Rows[i]["stock_old2"];
                r["namecat"] = dataSQL.Rows[i]["name_idcat"]; 
                
                data.Rows.Add(r);
            }
            return data;
        }
        public string FormatWeight(string str)
        {
            string size = "";
            string[] tam = str.Trim().Split('.');
            string phanle;
            if (tam.Length > 1) phanle = tam[1];
            else phanle = "";

            switch (tam[0].Length)
            {
                case 1:
                    if (tam[0] == "0") size = 0 + phanle;
                    else size = tam[0] + "L" + phanle;
                    break;
                case 2:
                    size = tam[0].Substring(0, 1) + "P"
                        + tam[0].Substring(1, 1) + "L" + phanle;
                    break;
                case 3:
                    size = tam[0].Substring(0, 1) + "C"
                        + tam[0].Substring(1, 1) + "P" +
                        tam[0].Substring(2, 1) + "L" + phanle;
                    break;
                case 4:
                    size = tam[0].Substring(0, 1) + "L" +
                        tam[0].Substring(1, 1) + "C"
                        + tam[0].Substring(2, 1) + "P" +
                        tam[0].Substring(3, 1) + "L" + phanle;
                    break;

            }
            return size;
        } 
    }
}
